/**
 * Loads Feeds Categories and fullfill a Select
 * 
 * @param jQuery box  The Select object to fullfill
 * @param String url to call 
 * @param String callback function to signal completed task
 * @return void
 */
function loadFeedCategory(box, url, callback) {
    jQuery.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        success: function(j) {
    		var options = '';
    		for (var i = 0; i < j.length; i++) {
    			options += '<option value="' + j[i].oid + '"';
    			options += '>' + j[i].name + '</option>';
    		}
    		box.html(options);
    		
    		if(callback != null) {
    			callback();
    		}
	    },
	    error: function(xhr, textStatus, errorThrown) {
           
        }
    });
}

/**
 * Loads Feeds and fullfill a Select
 * 
 * @param jQuery box  The Select object to fullfill
 * @param String url to call 
 * @param String callback function to signal completed task
 * @return void
 */
function loadFeeds(box, url, callback) {
    jQuery.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        success: function(j) {
    		var options = '';
    		for (var i = 0; i < j.length; i++) {
    			options += '<option value="' + j[i].oid + '" ';
    			options += 'title="' + j[i].url +'"';
    			options += '>' + j[i].name + '</option>';
    		}
    		
    		box.html(options);
    		
    		if(callback != null) {
    			callback();
    		}
	    },
	    error: function(xhr, textStatus, errorThrown) {
           
        }
    });
}

/**
 * Load all feeds beginning to a given category
 * Content is rendered as html options and injected in a select
 * passed in as parameter
 * 
 * @param jQuery box select object to inject in
 * @param String url to call
 * @param String callback function to signal completed task
 * @return void
 */
function loadFeedByCategory(box, url, callback) {
	 jQuery.ajax({
        type: "POST",
        url: url,
        data: {catID: box.val()},
        dataType: "json",
        success: function(j) {
        	var options = '';
    		for (var i = 0; i < j.length; i++) {
    			options += '<option value="' + j[i].oid + '" ';
    			options += 'title="' + j[i].url +'"';
    			options += '>' + j[i].name + '</option>';
    		}
    		
    		if(callback != null) {
    			callback(options);
    		}
	    },
	    error: function(xhr, textStatus, errorThrown) {

	    }
	 });
}

/**
 * Lock user account
 * 
 * @param int userID to lock
 * @param String url to call
 * @return void
 */
function lockUser(userID, url) {
	setFlag(userID, 0, url, function(data) {
		if (data.error) {
        	jQuery.facebox(data.message);
        } else {
            $('#lockBlock-'+userID).addClass('hidden');
            $('#unlockBlock-'+userID).removeClass('hidden');
        }
	});
}

/**
 * Unlock user account
 * 
 * @param int userID to unlock
 * @param String url to call
 * @return void
 */
function unlockUser(userID, url, callback) {
	setFlag(userID, 1, url, function(data) {
		if (data.error) {
            jQuery.facebox(data.message);
        } else {
            $('#lockBlock-'+userID).removeClass('hidden');
            $('#unlockBlock-'+userID).addClass('hidden');
        }
	});
}

/**
 * Delete a category
 * 
 * @param int catID to delete
 * @param String url to call
 * @param messages an array object containig messages for dialogbox
 * @return void
 */
function deleteCategory(catID, url, messages) {
	$('#confirmbox').dialog('option','buttons', {
        "OK": function() {
            $(this).dialog('close');
			executeRequest(catID, url, function(data) {
				if (data.error) {
					jQuery.facebox(data.message);
				} else {
					$('#ulBoxList-'+catID).effect('drop',{},600,function(){
			            $(this).remove();
			        });
				}
			});
		},
	    "NO": function() {
	        $(this).dialog('close');
	    }
	});

	showConfirmDialog($('#confirmbox'), messages);
}

/**
 * Show edit category form box
 * 
 * @param int catID to edit
 * @param String url to call
 * @return void
 */
function editCategory(catID, url) {
   jQuery.ajax({
        type: "POST",
        url: url,
        data: {catID: catID},
        success: function(data) {
	   		jQuery.facebox(data);
        },
        error: function(xhr, textStatus, errorThrown) {
           
        }
    });
}


/**
 * Lock user account
 * 
 * @param int userID to lock
 * @param String url to call
 * @return void
 */
function published(boxID, url) {
	setFlag(boxID, 1, url, function(data) {
		if (data.error) {
        	jQuery.facebox(data.message);
        } else {
            $('#lockBlock-'+boxID).removeClass('hidden');
            $('#unlockBlock-'+boxID).addClass('hidden');
        }
	});
}

/**
 * Unlock user account
 * 
 * @param int userID to unlock
 * @param String url to call
 * @return void
 */
function notPublished(boxID, url, callback) {
	setFlag(boxID, 0, url, function(data) {
		if (data.error) {
            jQuery.facebox(data.message);
        } else {
            $('#lockBlock-'+boxID).addClass('hidden');
            $('#unlockBlock-'+boxID).removeClass('hidden');
        }
	});
}

/**
 * Delete a box
 * 
 * @param int boxID to delete
 * @param String url to call
 * @param messages an array object containig messages for dialogbox
 * @return void
 */
function deleteBox(boxID, url, messages) {
	$('#confirmbox').dialog('option','buttons', {
        "OK": function() {
            $(this).dialog('close');
			executeRequest(boxID, url, function(data) {
				if (data.error) {
					jQuery.facebox(data.message);
				} else {
					$('#ulBoxList-'+boxID).effect('drop',{},600,function(){
			            $(this).remove();
			        });
				}
			});
		},
	    "NO": function() {
	        $(this).dialog('close');
	    }
	});

	showConfirmDialog($('#confirmbox'), messages);
}

/**
 * Delete a Feed
 * 
 * @param int feedID to delete
 * @param String url to call
 * @param messages an array object containig messages for dialogbox
 * @return void
 */
function deleteFeed(feedID, url, messages) {
	$('#confirmbox').dialog('option','buttons', {
        "OK": function() {
            $(this).dialog('close');
			executeRequest(feedID, url, function(data) {
				if (data.error) {
					jQuery.facebox(data.message);
				} else {
					$('#ulBoxList-'+feedID).effect('drop',{},600,function(){
			            $(this).remove();
			        });
				}
			});
		},
	    "NO": function() {
	        $(this).dialog('close');
	    }
	});
	
	showConfirmDialog($('#confirmbox'), messages);
}
